

package com.maoshi.shop.security.api.filter;

import com.maoshi.shop.security.api.model.MaoshiUser;
import com.maoshi.shop.security.comment.enums.App;
import com.maoshi.shop.security.comment.filter.MaoshiAuthenticationProcessingFilter;
import com.maoshi.shop.security.comment.token.AuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义登陆filter，直接返回一个用户的token，用于进行测试
 * 用于测试登录的token
 *       post:http://127.0.0.1:8086/debugToken
 * @author LGH
 */
@Component
public class DebuggerAuthenticationFilter extends MaoshiAuthenticationProcessingFilter {

    public DebuggerAuthenticationFilter() {
        super("/debugToken");
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {

        AuthenticationToken authenticationToken = new AuthenticationToken();

        MaoshiUser loadedUser = new MaoshiUser();
        loadedUser.setName("测试用户");
        loadedUser.setAppType(App.MINI.value());
        loadedUser.setUserId("1");
        loadedUser.setBizUserId("debuggerOpenId1");
        loadedUser.setEnabled(true);
        return handleAuthenticationToken(authenticationToken,loadedUser);
    }


}
